package com.hualan.sfzy.controller;


import com.hualan.sfzy.entity.SysUser;
import com.hualan.sfzy.service.SysUserService;
import com.hualan.sfzy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

import java.util.HashMap;


@RestController
@RequestMapping("/auth")
public class LoginController {


   @Autowired
   private SysUserService sysUserService;

    @PostMapping("/register")
    public Result register(@RequestBody SysUser entity) {
        SysUser user = sysUserService.findByUsername(entity.getUsername());
        if(user!=null)
            return Result.error();
        sysUserService.add(entity);
        return Result.success();
    }

    @PostMapping("/login")
    public Result login(@RequestParam String username, @RequestParam String userpwd, HttpServletRequest request) {
        SysUser user = sysUserService.findByUsername(username);
        if(user==null)
            return Result.error();
        if(!user.getUserpwd().equals(userpwd))
            return Result.error();
        System.out.println(user);
        System.out.println(username);
        System.out.println(userpwd);
        request.getSession().setAttribute("loginUserInfo", user);
        return Result.success(user);
    }

    @GetMapping("/logout")
    public Result logout(HttpServletRequest request) {
        request.getSession().removeAttribute("loginUserInfo");
        return Result.success();
    }
}
